import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import { ServerPort } from "./config.js";
import { sign, wechat } from "./middleware.js";
import { logger } from "./utils.js";
import xmlParser from "express-xml-bodyparser";
import path from "path";
import { answerMiddleware } from "./answer.js";
const app = express();
app.use(cors());
app.all("*", function (req, res, next) {
  //设置允许跨域的域名，*代表允许任意域名跨域
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "content-type"); //允许的header类型
  res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS"); //跨域允许的请求方式
  next(); //是否继续向下执行
});
//post传输数据类型
app.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
app.use(bodyParser.json());
app.use(xmlParser({ explicitArray: false, normalizeTags: false }));
app.use("/answer", answerMiddleware); // 操作自动回复
app.use("/static", logger, express.static(path.join(__dirname, "../static"))); // 静态资源
app.use("/", logger, sign, wechat);
app.listen(ServerPort, () => {
  console.log("Server Start~");
});
process.on("unhandledRejection", (reason, promise) => {
  console.error("Unhandled Rejection at:", promise, "reason:", reason);
});
